/*
 * @Author: 深海有鱼 847094299@qq.com
 * @Date: 2025-04-03 22:51:00
 * @LastEditors: 深海有鱼 847094299@qq.com
 * @LastEditTime: 2025-09-07 10:55:30
 * @FilePath: \pure-admin-thin\src\api\adminTaskApi.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { http } from "@/utils/http";
import { useStreamDownload } from "@/utils/useStreamDownload";

/** 分页获取所有任务（支持多条件筛选） */
export const getAllTasks = (params?: {
  page?: number;
  size?: number;
  userId?: number | string;
  serviceType?: string;
  country?: string;
  status?: string;
  keyword?: string;
  startDate?: string;
  endDate?: string;
  ownerId?: number;
}) => {
  return http.request<any>("get", "/api/admin/tasks", { params });
};

/** 管理员主动同步任务状态 */
export const syncAdminTaskStatus = (taskId: number) => {
  return http.request<any>("post", `/api/admin/tasks/${taskId}/resync`);
};
/** 管理员主动退款 */
export const adminRefund = (taskId: number) => {
  return http.request<any>("post", `/api/admin/tasks/${taskId}/refund`);
};

/** 获取按用户统计的任务数据 */
export const getTaskStatsByUser = (params?: {
  serviceType?: string;
  country?: string;
}) => {
  return http.request<any>("get", "/api/admin/tasks/stats/by-user", { params });
};

/** 获取按服务类型统计的任务数据 */
export const getTaskStatsByService = () => {
  return http.request<any>("get", "/api/admin/tasks/stats/by-service");
};

/** 获取按国家统计的任务数据 */
export const getTaskStatsByCountry = (params?: { serviceType?: string }) => {
  return http.request<any>("get", "/api/admin/tasks/stats/by-country", {
    params
  });
};

/** 导出筛选后的任务数据（CSV文件） */
export const exportTasks = (params?: {
  userId?: number | string;
  serviceType?: string;
  country?: string;
  status?: string;
}) => {
  const { download } = useStreamDownload();
  return download("/api/admin/tasks/export", {
    method: "GET",
    params
  });
};

/** 获取指定任务详情（管理员端） */
export const getAdminTaskDetail = (taskId: number) => {
  return http.request<any>("get", `/api/admin/tasks/${taskId}`);
};

/** 管理员直接下载任务结果文件 */
export const downloadAdminFilterResultFile = (
  taskId: number,
  params: { activeDaysThreshold?: string | number }
): Promise<void> => {
  const { download } = useStreamDownload();
  return download(`/api/admin/tasks/${taskId}/filtered-download`, {
    method: "GET",
    params
  });
};

export const downloadAdminAllResultFile = (
  taskId: number,
  params: { fileType?: string }
): Promise<void> => {
  const { download } = useStreamDownload();
  return download(`/api/admin/tasks/${taskId}/download-file`, {
    method: "GET",
    params
  });
};

/** 管理员直接下载任务结果文件 */
export const downloadAdminTaskResultFile = (
  taskId: number,
  params: { fileType: string }
): Promise<void> => {
  const { download } = useStreamDownload();
  return download(`/api/admin/tasks/${taskId}/download-file`, {
    method: "GET",
    params
  });
};

/** 获取任务结果的下载链接（管理员端） */
export const getAdminTaskResultUrl = (taskId: number) => {
  return http.request<any>("get", `/api/admin/tasks/${taskId}/download`);
};
